package js.android.nateroidbeta;

import java.util.ArrayList;

import rath.nateon.NateUser;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FriendList extends ListActivity {
	ArrayList<NateUser> friendsList = (ArrayList<NateUser>)NateOnSingleton.getFriendsList();
	
	  /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
    	try{
			super.onCreate(savedInstanceState);
			NateOnSingleton.SetStatus(UIStatus.OnList);
			setContentView(R.layout.friendlist);
				
			UpdateList();
    	}catch(Exception e)
    	{
    		TextView tv = new TextView(this);
    		tv.setText(e.getMessage());
    		setContentView(tv);
    	}
     }
    
    public void UpdateList()
    {
		ArrayList <BuddyListEntity> BUDDIES = new ArrayList<BuddyListEntity>();
		
//		ArrayList<NateUser> friendsList = (ArrayList<NateUser>)NateOnSingleton.getFriendsList();
				
		for(int i = 0; i < friendsList.size(); i++)
		{
			NateUser Friend = friendsList.get(i); 
			
			BUDDIES.add(new BuddyListEntity( Friend.getName(), Friend.getEmail(), Friend.getState(), Friend.getNickname()));
		}

		setListAdapter(new ArrayAdapter<BuddyListEntity>(this, android.R.layout.simple_list_item_1, BUDDIES)
				{
				@Override
					public View getView(int position, View convertView, ViewGroup parent) {
						BuddyListEntity Entity = (BuddyListEntity)getItem(position);
						TextView resultView = new TextView(super.getContext());
						
						/*//what is this code for?
						if (null == convertView || !(convertView instanceof TextView)) {
								resultView = new TextView(super.getContext());
						}
						*/
						
						try {
							resultView.setText(Entity.getState() + " " + Entity.getName() + ":" + Entity.getNickName());
						} catch (Exception e) {
							e.printStackTrace();
						}
						return resultView;
					}
				
					public void insert(BuddyListEntity Entity, int iPosition)
					{
						TextView resultView = new TextView(super.getContext());
						resultView.setText(Entity.getState() + " " + Entity.getName());
						getListView().addView(resultView, iPosition);
					}
				}
		);

		getListView().setTextFilterEnabled(true);
		getListView().setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick( AdapterView av, View v, int position, long id){
			//do stuff here
				try{
					BuddyListEntity Entity = (BuddyListEntity)getListView().getAdapter().getItem(position);
				
					if(NateOnSingleton.CallAndInvite(Entity.getEmail()) == true)
					{
						Intent myIntent = new Intent(v.getContext(), ChatScreen.class );
						startActivityForResult(myIntent,0);
					}
					else
					{
						AlertDialog.Builder msg = new AlertDialog.Builder(av.getContext());
						msg.setTitle("Debug");
						msg.setMessage("Invitaion Fail");
						msg.setPositiveButton("OK", null);
						msg.show();
					}
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				
			}
		});
	
    }
    
    public class BuddyListEntity
    {
    	public String m_strName;
    	public String m_strEmail;
    	public String m_strState;
    	public String m_strNickName;
    	
    	public BuddyListEntity(String strName, String strEmail, String strState, String strNickName)    		
    	{
    		m_strName = strName;
    		m_strEmail = strEmail;
    		m_strState = strState;
    		m_strNickName = strNickName;
    		
    	};
    	
    	public String getName()
    	{
    		return m_strName;
    	}
    	
    	public String getState()
    	{
    		return m_strState;
    	}
    	
    	public String getEmail()
    	{
    		return m_strEmail;
    	}
    	
    	public String getNickName()
    	{
    		return m_strNickName;
    	}
    	
    	public String ToString()
    	{
    		return m_strState + " " + m_strName;
    	}
    }
}
